package solution._0010._0017;

import java.util.*;

/**
 * Created by chen_snow
 * Date: 2021/5/11 19:33
 * Description: 电话号码的字母组合
 */
public class Solution {

    public static List<String> result = new ArrayList<>();

    public static void main(String[] args) {
        letterCombinations("23");
    }

    public static List<String> letterCombinations(String digits) {

        List<String> result = new ArrayList<>();

        if (digits == null || digits.isEmpty()) {
            return result;
        }

        ArrayList<String> data = new ArrayList<>();
        data.add("");
        data.add("");
        data.add("abc");
        data.add("def");
        data.add("ghi");
        data.add("jkl");
        data.add("mno");
        data.add("pqrs");
        data.add("tuv");
        data.add("wxyz");

        StringBuffer stringBuffer = new StringBuffer();
        ArrayList<List<Character>> digitsList = initList(data, digits);


        combination(digitsList, stringBuffer, 0, result);

        return result;
    }

    public static void combination(ArrayList<List<Character>> digitsList, StringBuffer stringBuffer, int index, List<String> result) {

        if (index == digitsList.size()) {
            result.add(stringBuffer.toString());
            return;
        }

        List<Character> characterList = digitsList.get(index);

        for (int i = 0; i < characterList.size(); i++) {
            stringBuffer.append(characterList.get(i));
            combination(digitsList, stringBuffer, index + 1, result);
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        }
    }


    public static ArrayList<List<Character>> initList(ArrayList<String> data, String digits) {
        ArrayList<List<Character>> result = new ArrayList<>();

        for (int i = 0; i < digits.length(); i++) {
            result.add(initData(data.get(digits.charAt(i) - '0')));
        }
        return result;
    }

    public static ArrayList<Character> initData(String data) {
        ArrayList<Character> temp = new ArrayList<>();
        for (int i = 0; i < data.length(); i++) {
            temp.add(data.charAt(i));
        }
        return temp;
    }


}
